package com.sxj.features.reflex;

import java.lang.reflect.Field;
import java.util.Collection;

import com.sxj.features.annotation.CxfDocd;

public class LogUtils {
	
	//static Log log = AppLogger.getLog(LogUtils.class);
	
	/*
	 * 获取当前对象的@cxf上的description注释，以及当前属性的值
	 * (如果要做日志详情，必须加上@CxfDoc(description="。。。。")注解)
	 */
	public static String getLogDetailInfo(Object clazz) {
		StringBuilder detail = new StringBuilder();
		try {
			if (clazz instanceof Collection) {
				Collection<?> collection = (Collection<?>) clazz;
				for (Object obj : collection) {
					detail.append("[" + getLogDetailInfo(obj) + "]");
				}
			} else {
				if (clazz == null) {
					return "";
				}
				Field[] fields = clazz.getClass().getDeclaredFields();
				for (Field field : fields) {
			        //获取注解Cxf中的description属性值
					CxfDocd cxf = field.getAnnotation(CxfDocd.class);
			        if (cxf == null) {
			        	continue;
			        }
			        String description = cxf.description();
			        field.setAccessible(true);
			        //只获取类型为String和List<String>类型的值，其他类型的属性继续循环遍历
			        if (field.getType().toString().equals("class java.lang.String") || 
			        		field.getGenericType().toString().equals("java.util.List<java.lang.String>")){
				        //通过反射获取属性的值
				        Object value = field.get(clazz);
				        if (value != null) {
				            value = value.toString();
				        } else {
				        	//如果值为空，则过滤该字段
				        	continue;
				        }
				        detail.append(description + ":" + value + "; ");
			        } else {
					    detail.append(description + ":[" +getLogDetailInfo(field.get(clazz)) + "]");
			        	continue;
			        }
				}
			}
		} catch (Exception e) {
			//log.error("参数拼装失败：" + e);
			e.printStackTrace();
		}
		return detail.toString();
	}
	
}
